package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.University;
import cn.wolfcode.crm.mapper.UniversityMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.UniversityQueryObject;
import cn.wolfcode.crm.service.IUniversityService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UniversityServiceImpl implements IUniversityService {
    @Autowired
    private UniversityMapper universityMapper;
    @Override
    public void deleteByPrimaryKey(Long id) {
        universityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(University entity) {
        universityMapper.insert(entity);
    }

    @Override
    public University selectByPrimaryKey(Long id) {

        return universityMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<University> selectAll() {
        return universityMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(University entity) {
        universityMapper.updateByPrimaryKey(entity);
    }

    public void changState(Long id) {
        universityMapper.changState(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer total = universityMapper.query4Count(qo);
        if(total==0){
            return PageResult.PAGERESULT_EMPTY;
        }
        return new PageResult(total,universityMapper.query4List(qo));
    }




    /**
     * 根据条件查询员工列表导出到exl
     *
     * @param qo
     * @return
     */
    @Override
    public Workbook exportXls(UniversityQueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("学校");
        row.createCell(2).setCellValue("地址");
        row.createCell(3).setCellValue("重要度");
        row.createCell(4).setCellValue("意向度");
        row.createCell(5).setCellValue("学校电话");
        row.createCell(6).setCellValue("意向学科");
        row.createCell(7).setCellValue("联系人");
        row.createCell(8).setCellValue("营销人员");
        row.createCell(9).setCellValue("跟进人员");
        row.createCell(10).setCellValue("上次跟进时间");
        row.createCell(11).setCellValue("下次跟进时间");
        row.createCell(12).setCellValue("跟进状态");
        row.createCell(13).setCellValue("客户状态");

        List<?> list = universityMapper.query4List(qo);

        for (int i = 0; i < list.size(); i++) {
            try {
                University university = (University) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(university.getId());
                row.createCell(1).setCellValue(university.getName());
                row.createCell(2).setCellValue(university.getAddress());
                row.createCell(3).setCellValue(university.getImportance() != null ? university.getImportance().getName() : "");
                row.createCell(4).setCellValue(university.getWantLevel() != null ? university.getWantLevel().getName() : "");
                row.createCell(5).setCellValue(university.getSchoolTel());
                row.createCell(6).setCellValue(university.getSubject() != null ? university.getSubject().getName() : "");
                row.createCell(7).setCellValue(university.getContact() != null ? university.getContact().getName() : "");
                row.createCell(8).setCellValue(university.getMarket() != null ? university.getMarket().getUsername() : "");
                row.createCell(9).setCellValue(university.getTrace() != null ? university.getTrace().getUsername() : "");
                row.createCell(10).setCellValue(university.getPrevTraceTime());
                row.createCell(11).setCellValue(university.getNextTraceTime());
                row.createCell(12).setCellValue(university.getTraceState() ? "已跟进" : "未跟进");
                row.createCell(13).setCellValue(university.getCustomerStatus() ? "已签约" : "签约");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return book;
    }



    /**
     * 用户获取获取模板
     *
     * @return
     */
    public Workbook getImportXlsMode() {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("学校");
        row.createCell(2).setCellValue("地址");
        row.createCell(3).setCellValue("重要度");
        row.createCell(4).setCellValue("意向度");
        row.createCell(5).setCellValue("学校电话");
        row.createCell(6).setCellValue("意向学科");
        row.createCell(7).setCellValue("联系人");
        row.createCell(8).setCellValue("营销人员");
        row.createCell(9).setCellValue("跟进人员");
        row.createCell(10).setCellValue("上次跟进时间");
        row.createCell(11).setCellValue("下次跟进时间");
        row.createCell(12).setCellValue("跟进状态");
        row.createCell(13).setCellValue("客户状态");
        return book;
    }




}
